/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.adhocracy.jira.cli;

import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.domain.BasicProject;
import com.atlassian.jira.rest.client.domain.Project;
import com.logiclander.adhocracy.cli.CommandLineReactor;
import java.util.Iterator;

/**
 *
 * @author tim
 */
public class JiraListProjectsReactor implements CommandLineReactor {

    private boolean initialized = false;
    final Iterable<BasicProject> projectList;
    final JiraRestClient restClient;
    final NullProgressMonitor progressMonitor;

    public JiraListProjectsReactor(Iterable<BasicProject> pl, JiraRestClient jrc, NullProgressMonitor pm) {
        projectList = pl;
        restClient = jrc;
        progressMonitor = pm;
    }

    public void init() {
        initialized = true;
        System.out.println("----------------------------------------------------------------------");
        System.out.println("-- ProjectList                                                      --");
        System.out.println("----------------------------------------------------------------------");
    }

    public void process() {
        for (Iterator i = projectList.iterator(); i.hasNext();) {
            BasicProject bp = (BasicProject) i.next();
            Project expanded = restClient.getProjectClient().getProject(bp.getKey(), progressMonitor);
            if (expanded != null) {
                System.out.println("        key: " + expanded.getKey());
                System.out.println("description: " + expanded.getDescription());
                System.out.println("       lead: " + expanded.getLead().getName());
                System.out.println("----------------------------------------------------------------------");
            } else {
                System.out.println("expanded project IS null");
            }
        }
    }

    public void destroy() {
        initialized = false;
    }
}
